package core.bmp.worker;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import core.bmp.Bmp;

public class TranslationWorker {

	public static void translation(String srcImageFile, String result) {
		BufferedImage bufferedimage;
		try {
			bufferedimage = ImageIO.read(new File(srcImageFile)); // 读入文件
			int w = bufferedimage.getWidth();
			int h = bufferedimage.getHeight();
			int type = bufferedimage.getColorModel().getTransparency();
			// 定义绘制变换类at1
			AffineTransform at1 = new AffineTransform();
			// 设置并应用平移变换,注意这里平移可以理解为是屏幕坐标系统的平移: 平移以后,屏幕坐标的原点位于(150,150)
			at1.setToTranslation(80.0, 50.0);

			BufferedImage relImg = new BufferedImage(w, h, type);
			Graphics2D g2d = relImg.createGraphics(); // 设置背景为透明
			relImg = g2d.getDeviceConfiguration().createCompatibleImage(w, h);
			g2d.dispose();
			g2d = relImg.createGraphics();
			g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			g2d.setTransform(at1);
			g2d.drawImage(bufferedimage, 0, 0, null);
			g2d.dispose();

			ImageIO.write(relImg, "BMP", new File(result));
		} catch (IOException e) {
		}
	}

	public static void main(String[] args) {
		translation(Bmp.BMP_LENA.getAbsolutePath(), new File(Bmp.DIR_FILE, "TranslationWoker.bmp").getAbsolutePath());
	}

}
